<?php
/**
 * @author Nguyen Van Anh
 * @mail anh_saker_no1@yahoo.com | anhsk.ohbo@gmail.com
 */

/**
 * nv_unhtmlspecialchars()
 * 
 * @param mixed $string
 * @return
 */
function nv_unhtmlspecialchars ( $string )
{
    if ( empty( $string ) ) return $string;
    if ( is_array( $string ) )
    {
        foreach ( array_keys( $string ) as $key )
        {
            $string[$key] = nv_unhtmlspecialchars( $string[$key] );
        }
    }
    else
    {
        $search = array( 
            '&amp;', '&#039;', '&quot;', '&lt;', '&gt;', '&#x005C;', '&#x002F;', '&#40;', '&#41;', '&#42;', '&#91;', '&#93;', '&#33;', '&#x3D;', '&#x23;', '&#x25;', '&#x5E;', '&#x3A;', '&#x7B;', '&#x7D;', '&#x60;', '&#x7E;' 
        );
        $replace = array( 
            '&', '\'', '"', '<', '>', '\\', '/', '(', ')', '*', '[', ']', '!', '=', '#', '%', '^', ':', '{', '}', '`', '~' 
        );
        $string = str_replace( $search, $replace, $string );
    }
    
    return $string;
}

/**
 * nv_htmlspecialchars()
 * 
 * @param mixed $string
 * @return
 */
function nv_htmlspecialchars ( $string )
{
    if ( empty( $string ) ) return $string;
    if ( is_array( $string ) )
    {
        foreach ( array_keys( $string ) as $key )
        {
            $string[$key] = nv_htmlspecialchars( $string[$key] );
        }
    }
    else
    {
        $search = array( 
            '&', '\'', '"', '<', '>', '\\', '/', '(', ')', '*', '[', ']', '!', '=', '%', '^', ':', '{', '}', '`', '~' 
        );
        $replace = array( 
            '&amp;', '&#039;', '&quot;', '&lt;', '&gt;', '&#x005C;', '&#x002F;', '&#40;', '&#41;', '&#42;', '&#91;', '&#93;', '&#33;', '&#x3D;', '&#x25;', '&#x5E;', '&#x3A;', '&#x7B;', '&#x7D;', '&#x60;', '&#x7E;' 
        );
        $string = str_replace( $replace, $search, $string );
        $string = str_replace( "&#x23;", "#", $string );
        $string = str_replace( $search, $replace, $string );
        $string = preg_replace( "/([^\&]+)\#/", "\\1&#x23;", $string );
    }
    
    return $string;
}


/**
 * nv_nl2br()
 * 
 * @param mixed $text
 * @param string $replacement
 * @return
 */
function nv_nl2br ( $text, $replacement = '<br />' )
{
    if ( empty( $text ) ) return '';
    return strtr( $text, array( 
        "\r\n" => trim( $replacement ), "\r" => trim( $replacement ), "\n" => trim( $replacement ) 
    ) );
}

/**
 * nv_br2nl()
 * 
 * @param mixed $text
 * @return
 */
function nv_br2nl ( $text )
{
    if ( empty( $text ) ) return '';
    return preg_replace( '/\<br(\s*)?\/?(\s*)?\>/i', chr( 13 ) . chr( 10 ), $text );
}

/**
 * nv_editor_nl2br()
 * 
 * @param mixed $text
 * @return
 */
function nv_editor_nl2br ( $text )
{
    if ( empty( $text ) ) return '';
    $replacement = defined( 'NV_EDITOR' ) ? '' : '<br />';
    return strtr( $text, array( 
        "\r\n" => trim( $replacement ), "\r" => trim( $replacement ), "\n" => trim( $replacement ) 
    ) );
}

/**
 * nv_editor_br2nl()
 * 
 * @param mixed $text
 * @return
 */
function nv_editor_br2nl ( $text )
{
    if ( empty( $text ) ) return '';
    if ( defined( 'NV_EDITOR' ) ) return $text;
    return preg_replace( '/\<br(\s*)?\/?(\s*)?\>/i', chr( 13 ) . chr( 10 ), $text );
}

/**
 * filter_text_input()
 * 
 * @param mixed $inputname
 * @param string $mode
 * @param string $default
 * @param bool $specialchars
 * @param integer $maxlength
 * @param mixed $preg_replace
 * @return
 */
function filter_text_input ( $inputname, $mode = 'request', $default = '', $specialchars = false, $maxlength = 0, $preg_replace = array() )
{
    global $nv_Request;
    
    $value = $nv_Request->get_string( $inputname, $mode, $default );
    $value = strip_tags( $value );
    if ( ( bool )$specialchars == true )
    {
        $value = nv_htmlspecialchars( $value );
    }
    if ( ( int )$maxlength > 0 )
    {
        $value = nv_substr( $value, 0, $maxlength );
    }
    if ( ! empty( $preg_replace ) )
    {
        if ( isset( $preg_replace['pattern'] ) and ! empty( $preg_replace['pattern'] ) and isset( $preg_replace['replacement'] ) )
        {
            $value = preg_replace( $preg_replace['pattern'], $preg_replace['replacement'], $value );
        }
    }
    return trim( $value );
}

/**
 * filter_text_textarea()
 * 
 * @param mixed $inputname
 * @param string $default
 * @param string $allowed_html_tags
 * @param bool $save
 * @param string $nl2br_replacement
 * @return
 */
function filter_text_textarea ( $inputname, $default = '', $allowed_html_tags = '', $save = false, $nl2br_replacement = '<br />' )
{

    $value = $inputname;
    if ( empty( $value ) ) return $value;
    if ( ! empty( $allowed_html_tags ) )
    {
        $allowed_html_tags = array_map( "trim", explode( ",", $allowed_html_tags ) );
        $allowed_html_tags = "<" . implode( "><", $allowed_html_tags ) . ">";
        $value = strip_tags( $value, $allowed_html_tags );
    }
    if ( ( bool )$save ) $value = nv_nl2br( $value, $nl2br_replacement );
    return $value;
}

/**
 * nv_editor_filter_textarea()
 * 
 * @param mixed $inputname
 * @param string $default
 * @param string $allowed_html_tags
 * @param bool $save
 * @param string $nl2br_replacement
 * @return
 */
function nv_editor_filter_textarea ( $inputname, $default = '', $allowed_html_tags = '', $save = false, $nl2br_replacement = '<br />' )
{
 
    $value = $inputname;
    
    if ( strip_tags( $value ) == '' ) return '';
    
    if ( ! empty( $allowed_html_tags ) )
    {
        $allowed_html_tags = array_map( "trim", explode( ",", $allowed_html_tags ) );
        $allowed_html_tags = "<" . implode( "><", $allowed_html_tags ) . ">";
        $value = strip_tags( $value, $allowed_html_tags );
    }
    if ( ( bool )$save )
    {
        $value = nv_editor_nl2br( $value, $nl2br_replacement );
    }
    return $value;
}

// echo ini_get( 'upload_max_filesize' );


?>

<?php if(is_login() ): ?>
<form action="" method="post">

    <table class="table_updates" cellpadding="0" cellspacing="0" border="0" width="100%">
    
        <tr>
            <td width="150" valign="top" >
				<div>
					<img style="width: 50px; !impotant" src='<?php echo LIB.'/images/member_128.jpg' ?>' />
				</div>
            </td>
            <td colspan="2"><textarea class="update_form"></textarea></td>
        </tr>
        
        <tr>
            <td>&nbsp;</td>
            <td width="360"></td>
            <td class="td_submit_update"><input class="red" type="submit" name="submits" value="Update" /></td>
        </tr>
    
    </table>
    
</form>
<?php endif; ?>

<?php

if(isset($_POST['submits'])) alert('Xin chào các bạn!');

?>